package cn.com.dhc.danlu.widget.couponview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.LinearLayout;

import cn.com.dhc.danlu.R;

/**
 * @author: 李一方
 * @time: 2016-09-08 15:45
 * @mail: liyifang@danlu.com
 * @desc: 四边锯齿卡片视图
 */
public class SawtoothView extends LinearLayout {
    private final static boolean VISIBLE = false;
    private final static float CIRCLE_GAP = 0;
    private final static float CIRCLE_RADIUS = 2;
    private final static float SIDE_GAP = 0;

    private Paint mPaint;
    /**
     * 圆间距
     */
    private float gap;
    /**
     * 半径
     */
    private float radius;
    /**
     * 圆数量
     */
    private int wCircleNum;
    private int hCircleNum;
    private float wRemain;
    private float hRemain;
    /**
     * 距离边框的距离,在圆之间存在间隔时,值为0
     */
    private float sideGap;
    /**
     * 显示那一边的锯齿
     */
    private boolean showSides;
    private boolean showRightSide;
    private boolean showLeftSide;
    private boolean showTopSide;
    private boolean showBottomSide;

    public SawtoothView (Context context) {
        super (context);
    }

    public SawtoothView (Context context, AttributeSet attrs) {
        super (context, attrs);
        init (attrs);
    }

    private void init (AttributeSet attrs) {
        initPaint ();

        initAttrs (attrs);
    }

    private void initPaint () {
        mPaint = new Paint (Paint.ANTI_ALIAS_FLAG);
        mPaint.setDither (true);
        mPaint.setColor (Color.WHITE);
        mPaint.setStyle (Paint.Style.FILL);
    }

    private void initAttrs (AttributeSet attrs) {
        TypedArray ta = getContext ().obtainStyledAttributes (attrs, R.styleable.SawtoothView);
        showSides = ta.getBoolean (R.styleable.SawtoothView_showSides, true);
        if (showSides) {
            showRightSide = ta.getBoolean (R.styleable.SawtoothView_showRightSide, VISIBLE);
            showLeftSide = ta.getBoolean (R.styleable.SawtoothView_showLeftSide, VISIBLE);
            showTopSide = ta.getBoolean (R.styleable.SawtoothView_showTopSide, VISIBLE);
            showBottomSide = ta.getBoolean (R.styleable.SawtoothView_showBottomSide, VISIBLE);
        }
        gap = ta.getDimensionPixelSize (R.styleable.SawtoothView_gap, (int) TypedValue.applyDimension (TypedValue.COMPLEX_UNIT_DIP, CIRCLE_GAP, getContext ().getResources ().getDisplayMetrics ()));
        radius = ta.getDimensionPixelSize (R.styleable.SawtoothView_radius, (int) TypedValue.applyDimension (TypedValue.COMPLEX_UNIT_DIP, CIRCLE_RADIUS, getContext ().getResources ().getDisplayMetrics ()));
        sideGap = ta.getDimensionPixelSize (R.styleable.SawtoothView_sideGap, (int) TypedValue.applyDimension (TypedValue.COMPLEX_UNIT_DIP, SIDE_GAP, getContext ().getResources ().getDisplayMetrics ()));
        ta.recycle ();
    }

    public void setShowSides (boolean showSides) {
        this.showSides = showSides;
    }

    public void setShowRightSide (boolean showRightSide) {
        this.showRightSide = showRightSide;
    }

    public void setShowLeftSide (boolean showLeftSide) {
        this.showLeftSide = showLeftSide;
    }

    public void setShowTopSide (boolean showTopSide) {
        this.showTopSide = showTopSide;
    }

    public void setShowBottomSide (boolean showBottomSide) {
        this.showBottomSide = showBottomSide;
    }

    public void setGap (float gap) {
        this.gap = gap;
    }

    @Override
    protected void onSizeChanged (int w, int h, int oldw, int oldh) {
        super.onSizeChanged (w, h, oldw, oldh);
        if (wRemain == 0) {
            wRemain = (int) (w - gap - sideGap * 2) % (2 * radius + gap);
        }

        if (hRemain == 0) {
            hRemain = (int) (h - gap - sideGap * 2) % (2 * radius + gap);
        }

        wCircleNum = (int) ((w - gap - sideGap * 2) / (2 * radius + gap));
        hCircleNum = (int) ((h - gap - sideGap * 2) / (2 * radius + gap));
    }

    @Override
    protected void onDraw (Canvas canvas) {
        super.onDraw (canvas);

        for (int i = 0; i < wCircleNum; i++) {
            float x = sideGap + gap + radius + wRemain / 2 + ((gap + radius * 2) * i);

            if (showSides && showTopSide) {
                canvas.drawCircle (x, 0, radius, mPaint);
            }

            if (showSides && showBottomSide) {
                canvas.drawCircle (x, getHeight (), radius, mPaint);
            }
        }

        for (int i = 0; i < hCircleNum; i++) {
            float x = sideGap + gap + radius + hRemain / 2 + ((gap + radius * 2) * i);
            if (showSides && showLeftSide) {
                canvas.drawCircle (0, x, radius, mPaint);
            }

            if (showSides && showRightSide) {
                canvas.drawCircle (getWidth (), x, radius, mPaint);
            }
        }
    }
}